//
//  AnimationViewController.m
//  UniverseProject
//
//  Created by king on 2021/6/22.
//  Copyright © 2021 Cloud2209. All rights reserved.
//

#import "AnimationViewController.h"

@interface AnimationViewController ()

@property (nonatomic, weak)UIView *boxView2;
@property (nonatomic, weak)UIView *boxView1;


@property (nonatomic, weak)UIView *boxViewb;
@property (nonatomic, assign)CGFloat scaleRate;

@end

@implementation AnimationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *boxView = [[UIView alloc] initWithFrame:CGRectMake(10, 50, K_SCREEN_WIDTH, 400)];
    boxView.backgroundColor = [UIColor redColor];
    [self.view addSubview:boxView];
//    boxView.hidden = YES;
    self.boxView2 = boxView;
    
    UIView *boxView1 = [[UIView alloc] initWithFrame:CGRectMake(10, boxView.bottom - 100, 100, 100)];
    boxView1.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:boxView1];
    self.boxView1 = boxView1;
    boxView1.hidden = YES;
    
    [self setUpNavRightItem:@"收缩"];
    
    self.scaleRate = (boxView.height - boxView1.height) / boxView.height;
    
}

- (void)navigationRightItemClick{
   [self box1shousuo];
   [self box2zhankai];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self box1zhankai];
    [self box2shousuo];

    
}






//黄块
- (void)box1zhankai{
    //锚点设置之后,会导致frame改变,所以要重新计算
    CGPoint oldOrigin = self.boxView1.frame.origin;
    CGPoint anchorPoint = CGPointMake(0, 1);
    self.boxView1.layer.anchorPoint = anchorPoint;
    CGPoint newOrigin = self.boxView1.frame.origin;
    CGPoint transition;
    transition.x = newOrigin.x - oldOrigin.x;
    transition.y = newOrigin.y - oldOrigin.y;
    self.boxView1.transform = CGAffineTransformMakeScale(0.01, 0.01);
    self.boxView1.center = CGPointMake (self.boxView1.center.x - transition.x, self.boxView1.center.y - transition.y);
    self.boxView1.hidden = NO;
    [UIView animateWithDuration:1.f animations:^{
        self.boxView1.transform = CGAffineTransformMakeScale(1, 1);
    } completion:^(BOOL finished) {
    }];
}

//黄块
- (void)box1shousuo{
    //锚点设置之后,会导致frame改变,所以要重新计算
    CGPoint oldOrigin = self.boxView1.frame.origin;
    CGPoint anchorPoint = CGPointMake(0, 1);
    self.boxView1.layer.anchorPoint = anchorPoint;
    CGPoint newOrigin = self.boxView1.frame.origin;
    CGPoint transition;
    transition.x = newOrigin.x - oldOrigin.x;
    transition.y = newOrigin.y - oldOrigin.y;
    self.boxView1.center = CGPointMake (self.boxView1.center.x - transition.x, self.boxView1.center.y - transition.y);
    self.boxView1.hidden = NO;
    self.boxView1.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:1.f animations:^{
        self.boxView1.transform = CGAffineTransformMakeScale(0.01, 0.01);
    } completion:^(BOOL finished) {
        
    }];
}

//红块
- (void)box2zhankai{
    //锚点设置之后,会导致frame改变,所以要重新计算
    CGPoint oldOrigin = self.boxView2.frame.origin;
    CGPoint anchorPoint = CGPointMake(0, 0);
    self.boxView2.layer.anchorPoint = anchorPoint;
    CGPoint newOrigin = self.boxView2.frame.origin;

    CGPoint transition;
    transition.x = newOrigin.x - oldOrigin.x;
    transition.y = newOrigin.y - oldOrigin.y;
    self.boxView2.center = CGPointMake (self.boxView2.center.x - transition.x, self.boxView2.center.y - transition.y);
    [UIView animateWithDuration:1.f animations:^{
        self.boxView2.transform = CGAffineTransformMakeScale(1, 1);
    } completion:^(BOOL finished) {
        
    }];
}


//红块
- (void)box2shousuo{
    //锚点设置之后,会导致frame改变,所以要重新计算
    CGPoint oldOrigin = self.boxView2.frame.origin;
    CGPoint anchorPoint = CGPointMake(0, 0);
    self.boxView2.layer.anchorPoint = anchorPoint;
    CGPoint newOrigin = self.boxView2.frame.origin;
    CGPoint transition;
    transition.x = newOrigin.x - oldOrigin.x;
    transition.y = newOrigin.y - oldOrigin.y;
    self.boxView2.transform = CGAffineTransformMakeScale(1, 1);
    self.boxView2.center = CGPointMake (self.boxView2.center.x - transition.x, self.boxView2.center.y - transition.y);
    [UIView animateWithDuration:1.f animations:^{
        self.boxView2.transform = CGAffineTransformMakeScale(self.scaleRate, self.scaleRate);
    } completion:^(BOOL finished) {
        
    }];
}




@end
